using System;
using System.Collections;

namespace Jonathan
{
    public class ListItemCollection : CollectionBase
    {
        private ListControl m_ListControl;

        public ListItemCollection(ListControl lisControl)
        {
            this.m_ListControl = lisControl;
        }

        
        #region Add

        /// <summary>
        /// Adds a ListItem to the collection...
        /// </summary>
        /// <param name="page">ListItem to add...</param>
        /// <returns>Added ListItem's index</returns>
        public int Add(ListItem listItem)
        {
            // Make sure the listItem is not null...
            if (listItem == null)
            {
                throw new ArgumentNullException("ListItem", "ListItem is null!");
            }

            // Add the listItem...
            int index = this.List.Add(listItem);

            // Raise and Event...
            this.OnListItemAdded();

            return index;
        }

        /// <summary>
        /// Adds a ListItem to the collection...
        /// </summary>
        /// <param name="text">Text for new ListItem</param>
        /// <returns>Added ListItem's index</returns>
        public int Add(string text)
        {
            // Add the listItem...
            int index = this.List.Add(new ListItem(text));

            // Raise and Event...
            this.OnListItemAdded();

            return index;
        }

        /// <summary>
        /// Adds a ListItem to the collection...
        /// </summary>
        /// <param name="text">Text for new ListItem</param>
        /// <param name="value">Value for new ListItem</param>
        /// <returns>Added ListItem's index</returns>
        public int Add(string text, object value)
        {
            // Create a new ListItem...
            ListItem l = new ListItem(text);
            l.Value = value;

            // Add the listItem...
            int index = this.List.Add(l);

            // Raise and Event...
            this.OnListItemAdded();

            return index;
        }

        /// <summary>
        /// Adds a range of ListItems to the Collection...
        /// </summary>
        /// <param name="pages">Array of ListItem's to add...</param>
        public void AddRange(ListItem[] listItems)
        {
            // Make sure the pages are not null...
            if (listItems == null)
            {
                throw new ArgumentNullException("ListItem", "ListItems are null!");
            }

            // Add each page...
            for (int a = 0; a < listItems.Length; a++)
            {
                this.Add(listItems[a]);
            }
        }

        #endregion

        #region Remove

        /// <summary>
        /// Removes a ListItem from the Collection...
        /// </summary>
        /// <param name="page">ListItem to remove...</param>
        public void Remove(ListItem listItem)
        {
            // Get the index of the listItem...
            int index = this.IndexOf(listItem);

            // Make sure it is a valid index...
            if (index != -1)
            {
                // Remove Page...
                this.RemoveAt(index);

                // Raise an Event...
                this.OnListItemRemoved();
            }
        }

        /// <summary>
        /// Removes a range of ListItems from the collection...
        /// </summary>
        /// <param name="pages">Array of ListItems to be removed...</param>
        public void RemoveRange(ListItem[] listItems)
        {
            if (listItems == null)
            {
                throw new ArgumentNullException("ListItem", "ListItems are null!");
            }

            for (int a = 0; a < listItems.Length; a++)
            {
                this.Remove(listItems[a]);
            }
        }

        /// <summary>
        /// Removes all ListItems from the collection...
        /// </summary>
        public new void Clear()
        {
            if (this.Count == 0)
            {
                // Nothing to do, return...
                return;
            }

            // Clear the list...
            base.Clear();

            // Rest the Capacity...
            this.InnerList.Capacity = 0;

            // Raise an event
            this.OnListItemRemoved();
        }

        /// <summary>
        /// Gets the index of a specified ListItem...
        /// </summary>
        /// <param name="page">ListItem to get index for...</param>
        /// <returns>Index of ListItem</returns>
        public int IndexOf(ListItem listItem)
        {
            // Look for the ListItem...
            for (int a = 0; a < this.Count; a++)
            {
                if (this[a] == listItem)
                {
                    // Found it...
                    return a;
                }
            }

            // Not found...
            return -1;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a ListItem at the requested Index...
        /// </summary>
        /// <param name="index">Index of ListItem...</param>
        /// <returns>ListItem at requested Index...</returns>
        public ListItem this[int index]
        {
            get
            {
                // Make sure it is a valid index...
                if (index < 0 || index >= this.Count)
                {
                    return null;
                }

                // Return the tabber page...
                return this.List[index] as ListItem;
            }
        }

        /// <summary>
        /// Gets the WebViewer Associtated with this collection...
        /// </summary>
        public ListControl ListControl
        {
            get { return this.m_ListControl; }
        }

        #endregion

        #region Events

        protected internal void OnListItemAdded()
        {
            this.m_ListControl.CheckScroll();
        }

        protected internal void OnListItemRemoved()
        {
            this.m_ListControl.CheckScroll();
        }

        #endregion
    }
}
